package Client.Manager
{
	import flash.utils.getDefinitionByName;
	
	/**
	 * class		ResourceMgr
	 * @desc		used to manage the resources
	 * @author		Aqua
	 * @date		2011-9-5
	 */
	public class ResourceMgr
	{
		//---------------------//
		//      Singleton      //
		//---------------------//
		
		/**************************************************************************
		 * function					instance getter
		 * @desc					a private function used to enforce singlton
		 * @return	ResourceMgr		singleton instance of this class
		 *************************************************************************/
		public static function get instance():ResourceMgr
		{
			if(null == _instance)
			{
				_instance = new ResourceMgr(singleton);
			}
		
			return _instance;
		}
		
		/**************************************************************************
		 * function			singleton
		 * @desc			a private function used to enforce singlton
		 *************************************************************************/
		private static function singleton():void
		{
			
		}
		
		
		//---------------------//
		//     Constructor     //
		//---------------------//
		
		/**************************************************************************
		 * function			ResourceMgr
		 * @desc			constructor of class
		 * @param	s		a private function used to enforce singlton
		 *************************************************************************/
		public function ResourceMgr(s:Function = null):void
		{
			if (singleton != s)
			{
				throw new Error("This is a singleton class!");
			}
			
			resPool = {};
		}
		
		
		//-----------------//
		//     Publics     //
		//-----------------//
		
		/**************************************************************************
		 * function				getResource
		 * @desc				get resource by class's name
		 * @param	className	the name of resource's class
		 * @return	*			required resource, such as a xml file or an image
		 *************************************************************************/
		public function getResource(className:String):*
		{
			var cls:Class;
			
			if (null == resPool[className])
			{
				cls = getDefinitionByName(className);
			
				if (null != cls)
				{
					resPool[className] = new cls();
				}
				else
				{
					// log
				}
			}
			
			return resPool[className];
		}
		
		/**************************************************************************
		 * function				destroyResource
		 * @desc				destroy resource by class's name
		 * @param	className	the name of resource's class
		 *************************************************************************/
		public function destroyResource(className:String):void
		{
			if (null != resPool[className])
			{
				resPool[className] = null;
			}
		}
		
		//------------------//
		//     Privates     //
		//------------------//
		
		
		
		private static var _instance:ResourceMgr;
		
		private var resPool:Object;
		
		
		public static const XML_COMMAND	:String = "commandXml";
		public static const XML_UNIT	:String = "unitXml";
		public static const XML_EVENT	:String = "eventXml";
		public static const XML_AREA	:String = "areaXml";
		public static const XML_CARD	:String = "cardXml";
		public static const XML_HOUSE	:String = "houseXml";
		public static const XML_MAP		:String = "mapXml";
		
		[Embed(source = '../../../resource/xml/commands.xml')]
		public var commandXml:Class;
		[Embed(source = '../../../resource/xml/units.xml')]
		public var unitXml:Class;
		[Embed(source = '../../../resource/xml/events.xml')]
		public var eventXml:Class;
		[Embed(source = '../../../resource/xml/areas.xml')]
		public var areaXml:Class;
		[Embed(source = '../../../resource/xml/cards.xml')]
		public var cardXml:Class;
		[Embed(source = '../../../resource/xml/houses.xml')]
		public var houseXml:Class;
		[Embed(source = '../../../resource/xml/maps.xml')]
		public var mapXml:Class;
	}
	
}